﻿using System;
using AccessControl.Contracts.Models;
using AccessControl.Contracts.Settings;
using System.Collections;
using System.Collections.Generic;
using Action = AccessControl.Contracts.Models.Action;
using AccessControl.Data.Models;
using System.Windows.Input;
using AccessControl.Wpf.Commands;
using System.Collections.ObjectModel;
using AccessControl.Gateway.Managers;

namespace AccessControl.Wpf.Settings.ViewModels
{
	class OperatorsSettingsViewModel : SettingsBaseViewModel<IOperator>
	{
		#region Fields

		ObservableCollection<Action> _AvailableActions = new ObservableCollection<Action>();

		#endregion

		#region Public Constructor

		public OperatorsSettingsViewModel(ISettingsService settingsService) 
			: base(settingsService.Operators)
		{
			Name = "Operators";
		}

		#endregion

		#region Public Properties

		public IList<Action> AvailableActions
		{
			get { return _AvailableActions; }
		}

		#endregion

		#region SettingsBaseViewModel Overrides

		public override void AddItem(IOperator item)
		{
			SettingsService.Add(item);
		}

		public override void EditItem(IOperator item)
		{
			SettingsService.Update(item);
		}

		public override void DeleteItem(IOperator item)
		{
			SettingsService.Delete(item);
		}

		public override bool IsValidItem(IOperator item)
		{
			if (string.IsNullOrEmpty(item.UserName))
			{
				MessagingService.Show("Operator name cannot be empty", "AccessControl", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
				return false;
			}

			foreach (IOperator oper in SettingsService.Items)
			{
				if (oper.UserName.Equals(item.UserName) && oper != item)
				{
					MessagingService.Show("Operator name already exists", "AccessControl", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
					return false;
				}
			}

			return true;
		}

		public override IOperator CreateNewItem()
		{
			return new Operator()
			{
				UserName = string.Format("New Operator({0})", SettingsService.Items.Count + 1),
				Password = string.Empty
			};
		}

		#endregion

		#region Overrides

		protected override void OnCurrentItemChanged()
		{
			AvailableActions.Clear();
			AvailableActions.Add(Action.Operations);
			AvailableActions.Add(Action.Setup);
			AvailableActions.Add(Action.Reports);

			if (CurrentItem != null)
			{
				foreach (Action action in CurrentItem.AllowedActions)
					AvailableActions.Remove(action);
			}
		}

		protected override bool IsItemCanBeEdited(IOperator item)
		{
			if (item.Id == Session.Instance.LoggedInOperator.Id)
			{
				MessagingService.Show("Cannot edit the current logged in operator", "AccessControl", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Stop);
				return false;
			}

			return true;
		}

		#endregion

		#region Commands

		public ICommand AddAllowedActionCommand
		{
			get { return new CommandBaseOfT<Action>(AddAllowedAction); }
		}

		public ICommand RemoveAllowedActionCommand
		{
			get { return new CommandBaseOfT<Action>(RemoveAllowedAction); }
		}

		#endregion

		#region Private Members

		private void AddAllowedAction(Action action)
		{
			AvailableActions.Remove(action);
			CurrentItem.AllowedActions.Add(action);
		}

		private void RemoveAllowedAction(Action action)
		{
			AvailableActions.Add(action);
			CurrentItem.AllowedActions.Remove(action);
		}

		#endregion
	}
}
